/**   
* @Title: ModifyPhoneActivity.java 
* @Package com.jiuyang.baoxian.activity 
* @Description: TODO(用一句话描述该文件做什么) 
* @author ChangJianlong   
* @date 2014-12-16 下午5:33:54 
* @version V1.0   
*/
package com.bxd.baodan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.util.ETUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.NetUtil.RequestStringListener;
import com.bxd.baodan.util.ResourcesUtil;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.observer.SmsContent;

/** 
 * @ClassName: ModifyPhoneActivity 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author ChangJianlong 
 * @date 2014-12-16 下午5:33:54 
 *  
 */
public class ModifyPhoneActivity extends BaseActivity implements OnClickListener {

	
	private EditText etAccount, etVcode;
	private Button btnGetVcode;
	private SmsContent content;
	private Boolean isGetVcode = false;
	private int time = -1;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_tel);
		
		setActionBarTitle(ResourcesUtil.getStringById(R.string.ab_title_change_tel));
		setActionBarButton(ResourcesUtil.getStringById(R.string.done), new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//根据验证码结果进行跳转
				attempChangeTelNo();
			}
		});
		
		initView();
	}
	
	private void initView() {
		etAccount = getEditText(R.id.account_text);
		etVcode = getEditText(R.id.regist_vcode_edit);
		btnGetVcode = getButton(R.id.get_vcode_btn);
		
		btnGetVcode.setOnClickListener(this);
		
	}
	
	/**
	 * 开始计时
	 */
	private void startTime() {
		time = 60;
		isGetVcode = true;
		new Thread() {
			public void run() {
				do {
					try {
						Thread.sleep(1 * 1000);

						if (handler != null) {
							handler.sendEmptyMessage(--time);
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} while (time > 0 && isGetVcode == true);
				time = -1;
				handler.sendEmptyMessage(--time);
			}
		}.start();
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what > 0) {
				btnGetVcode.setText(msg.what + "秒后重新获取");
				btnGetVcode.setEnabled(false);
			} else {
				btnGetVcode.setText("重新获取验证码");
				btnGetVcode.setEnabled(true);
			}
		}
	};

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.get_vcode_btn:
			attemptIdentifyCode();
			break;

		default:
			break;
		}
	}
	
	/**
	 * 获取验证码
	 */
	private void attemptIdentifyCode() {
		if (TextUtils.isEmpty(getString(etAccount))) {
			etAccount.setError("号码不能为空");
			return;
		}
		if (!ETUtil.isMobileNO(etAccount)) {
			etAccount.setError("电话号码不合格");
			return;
		}

		NetUtil netUtil = new NetUtil(this, JsonApi.CP_VCODE);
		netUtil.setParams("phone", getString(etAccount));
		netUtil.setParams("src", "2");
		netUtil.postRequest(" ", new RequestStringListener() {
			@Override
			public void onComplete(String result) {
				if (JSONUtil.isSuccess(result)) {
					Toast.makeText(ModifyPhoneActivity.this, "验证码已发送您手机,请查收", Toast.LENGTH_SHORT).show();
					startTime();
					  content = new SmsContent(ModifyPhoneActivity.this, new Handler(), etVcode);
				  // 注册短信变化监听
					  ModifyPhoneActivity.this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);

				} else {
					if (JSONUtil.getMessage(result).isEmpty()) {
						if (JSONUtil.getCode(result)== 4) {
							showToast("验证码发送频繁,稍后重试");
						}
					} else {
						showToast(JSONUtil.getMessage(result));
					}
					isGetVcode = false;
				}
			}

		});
	}
	
	/**
	 * 获取注册结果
	 */
	private void attempChangeTelNo() {
		if (TextUtils.isEmpty(getString(etAccount))) {
			etAccount.setError("号码不能为空");
			return;
		}
		if (!ETUtil.isMobileNO(etAccount)) {
			etAccount.setError("电话号码不合格");
			return;
		}
		if (TextUtils.isEmpty(getString(etVcode))) {
			etVcode.setError("验证码不能为空");
			return;
		}
		final String tel = getString(etAccount);
		NetUtil netUtil1 = new NetUtil(this, JsonApi.MODIFYPHONE);
		netUtil1.setParams("newphone", tel);
		netUtil1.setParams("vcode", getString(etVcode));
		netUtil1.postRequest("", new RequestStringListener() {

			@Override
			public void onComplete(String result) {

				if (JSONUtil.isSuccess(result)) {
					showToast("手机号码修改成功");
					//重新登录
//					LoginInfo.getInstance(ModifyPhoneActivity.this).cancelLogin(ModifyPhoneActivity.this);
					SpUtil.getInstance().savaString(SharedPreferanceKey.ACCOUNT, tel);
					sendBroadcast(new Intent(Constant.ACTION_SUCCESS_UPLOAD_INFOR));
					ModifyPhoneActivity.this.finish();
				} else {
					if (JSONUtil.getMessage(result).isEmpty()) {
						if (JSONUtil.getCode(result)== 4) {
							showToast("验证码发送频繁,稍后重试");
						}
					} else {
						showToast(JSONUtil.getMessage(result));
					}
					isGetVcode = false;
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (handler!= null) {
			handler.removeCallbacksAndMessages(null);
			handler = null;
		}
	}
}
